//
//  EmailViewController.m
//  TableView
//
//  Created by iMac-hxsd on 12-2-26.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "EmailViewController.h"

@implementation EmailViewController

@synthesize tableData;
@synthesize keys;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    // 表格数据
    NSArray *a = [[NSArray alloc] initWithObjects:@"a邮件1", @"a邮件2", @"a邮件3", @"a邮件4", nil];
    NSArray *b = [[NSArray alloc] initWithObjects:@"b邮件1", @"b邮件2", @"b邮件3", @"b邮件4", nil];
    NSArray *c = [[NSArray alloc] initWithObjects:@"c邮件1", @"c邮件2", @"c邮件3", @"c邮件4", nil];
    NSArray *d = [[NSArray alloc] initWithObjects:@"d邮件1", @"d邮件2", @"d邮件3", @"d邮件4", nil];
    NSArray *e = [[NSArray alloc] initWithObjects:@"e邮件1", @"e邮件2", @"e邮件3", @"e邮件4", nil];
    NSArray *f = [[NSArray alloc] initWithObjects:@"f邮件1", @"f邮件2", @"f邮件3", @"f邮件4", nil];
    NSArray *g = [[NSArray alloc] initWithObjects:@"g邮件1", @"g邮件2", @"g邮件3", @"g邮件4", nil];
    self.tableData = [NSDictionary dictionaryWithObjectsAndKeys:a, @"a", b, @"b", c, @"c", d, @"d", e, @"e", f, @"f", g, @"g", nil];

    // 数据表索引数组，并进行排序
    self.keys = [[self.tableData allKeys] sortedArrayUsingSelector:@selector(compare:)];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table View Methods
// 表格分组数量
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.keys count];
}

// 表格数据行数
- (NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection : (NSInteger) section
{
    NSString *key = [self.keys objectAtIndex:section];

    NSArray *dataArray = [self.tableData objectForKey:key];

    return [dataArray count];
}

// 表格数据
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [self.keys objectAtIndex:[indexPath section]];
    NSArray *dataArray = [self.tableData objectForKey:key];

    static NSString *tableIdentifier = @"tableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = [dataArray objectAtIndex:[indexPath row]];

    return cell;
}

// 表格组头部标题
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [self.keys objectAtIndex:section];
    return key;
}

// 表格索引
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.keys;
}

@end
